﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SCGILite.NetLib
{
    public class NetOutputStream : INetOutputStream
    {
        Stream stream = null;
        public NetOutputStream(MemoryStream ms)
        {
            stream = ms;
        }
        public NetOutputStream(FileStream ms)
        {
            _CanSyncRead = false;
            stream = ms;
        }
        public NetOutputStream(Stream ms, bool canSyncRead)
        {
            _CanSyncRead = canSyncRead;
            stream = ms;
        }
        bool _CanSyncRead = true;
        public bool CanSyncRead
        {
            get { return _CanSyncRead; }
        }

        public int Read(byte[] buf, int offset, int length)
        {
            return stream.Read(buf, offset, length);
        }
        public void BeginRead(byte[] buffer, int offset, int count, Action<int> endRead)
        {
            stream.BeginRead(buffer, offset, count, (ar) => {
                int a = 0;
                try
                {
                    a = stream.EndRead(ar);
                }
                catch
                {
                }
                endRead(a);
            }, null);
        }

        public void Close()
        {
            stream.Close();
        }
    }
}
